//
//  GYEmoticonTools.swift
//  Weibo
//
//  Created by vane on 16/8/19.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
let GYEmoticonsMaxCol = 7
let GYEmoticonsMaxRow = 3
let GYEmoticonsMaxCount = GYEmoticonsMaxCol*GYEmoticonsMaxRow - 1
class GYEmoticonTools: NSObject {
    
    static let shardTools:GYEmoticonTools = GYEmoticonTools()
     // 读取bundle文件
    lazy var emoticonsBundle: NSBundle = {
        
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        
        let bundle = NSBundle(path: path)!
        
        return bundle
       
    }()
    // 默认表情一维数组
    lazy var defaultEmoticons: [GYEmoticonModel] = {
        return self.getEmoticonsWithPath("default")
    }()
    
    // emoji表情一维数组
    lazy var emojiEmoticons: [GYEmoticonModel] = {
        return self.getEmoticonsWithPath("emoji")
    }()
    
    // 浪小花表情一维数组
    lazy var lxhEmoticons: [GYEmoticonModel] = {
        return self.getEmoticonsWithPath("lxh")
    }()
    
    lazy var allEmoticons: [[[GYEmoticonModel]]] = {
        return [
            self.getEmotionsGroupsWithEmoticons(self.defaultEmoticons),
            self.getEmotionsGroupsWithEmoticons(self.emojiEmoticons),
            self.getEmotionsGroupsWithEmoticons(self.lxhEmoticons)
        ]
    }()

    //一维数组
    func getEmoticonsWithPath(path: String)->[GYEmoticonModel]{
        
        let file = emoticonsBundle.pathForResource("\(path)/info.plist", ofType: nil)!
        
        let array = NSArray(contentsOfFile: file)!
        
        var tempArr:[GYEmoticonModel] = [GYEmoticonModel]()
        
        for dict in array{
            
            let model = GYEmoticonModel(dict: dict as! [String : AnyObject])
            
            model.path = "\(path)/"
            
            tempArr.append(model)
            
        }
        return tempArr
    }
    
    //二维数组
    func getEmotionsGroupsWithEmoticons(emoticons: [GYEmoticonModel])->[[GYEmoticonModel]]{
        
        let pageCount = (emoticons.count + GYEmoticonsMaxCount - 1)/GYEmoticonsMaxCount
        
        var tempArr:[[GYEmoticonModel]] = [[GYEmoticonModel]]()
        
        for i in 0..<pageCount {
            
            let location = i*GYEmoticonsMaxCount
            
            var length = GYEmoticonsMaxCount

            if location + length > emoticons.count {
                
                length = emoticons.count - location
            }
        
            let range = NSRange(location: location, length: length)
            
            let array = (emoticons as NSArray).subarrayWithRange(range) as![GYEmoticonModel]
            
            tempArr.append(array)
        }
        return tempArr
        
    }
    

}
